<template>
  <div>
    <basic-modal :modalOpt="modal" :footer="null" @closeModal="closeModal">
      <template>
        <basic-form
          :formInfos="s_FormInfo"
          :formDatas="s_formDatas"
          :rules="s_rules"
          @onSubmit="onSubmit"
          @onChangeType="onChangeType"
          @closeModal="closeModal"
        ></basic-form>
      </template>
    </basic-modal>
  </div>
</template>

<script>
import basicModal from '../common/basicModal'
import basicForm from '../common/basicForm'
import { getInfoByPid as getCustomerInfoByPid } from '@/views/app/biz/customer/customer'
import { getInfoByPid as getsaleGoodsInfoByPid } from '@/views/app/biz/salesGoods/salegoods'
import { getInfoByPid, saveInfo } from './customersalegoods'

export default {
  components: {
    basicModal,
    basicForm,
  },
  data() {
    return {
      loading: false,
      modal: {
        modalVisible: false,
        modalTitle: '新增品种',
        modalWidth: '1000px',
      },
      s_FormInfo: [
        { label: '客户单位', isDisabled: false, name: 'keHuPid', type: 'customer', placeholder: '请选择客户单位' },
        { label: '选择销售商品', isDisabled: false, name: 'xiaoShoucpPid', type: 'salegoods', placeholder: '请输入产品销售代码' },
        { label: '品名', isDisabled: true, name: 'pinMing', type: 'input', placeholder: '请输入品名' },
        { label: '注册证号', isDisabled: true, name: 'zhuCezh', type: 'customer', placeholder: '注册证号' },
        { label: '中标类型', isDisabled: false, name: 'zhongBiaolx', dicName: 'zhongBiaolx', type: 'dicselect', placeholder: '请选择品中标类型' },
        { label: '中标号', isDisabled: false, name: 'zhongBiaoh', type: 'input', placeholder: '请输入中标号' },
        { label: '无税单价(元)', isDisabled: false, name: 'wuShuidj', type: 'number', placeholder: '请输入不含税单价，可不填' },
        { label: '含税单价(元)', isDisabled: false, name: 'hanShuidj', type: 'number', placeholder: '请输入含税单价' },
        { label: '税率（百分比）', isDisabled: false, name: 'shuiLv', type: 'number', placeholder: '请输入税率（百分比）' },
        { label: '通用名', isDisabled: true, name: 'tongYongm', type: 'input', placeholder: '请输入通用名' },
        { label: '规格', isDisabled: true, name: 'guiGe', type: 'input', placeholder: '请输入' },
        { label: '型号', isDisabled: true, name: 'xingHao', type: 'input', placeholder: '请输入' },
        { label: '生产企业名称', isDisabled: true, name: 'shengChanqymc', type: 'input', placeholder: '请输入' },
        { label: '品牌', isDisabled: true, name: 'pingPai', type: 'input', placeholder: '请输入' },
        { label: '一级分类', isDisabled: true, name: 'yiJifldm', type: 'input', placeholder: '请输入一级分类代码' },
        // { label: '一级分类名称',  isDisabled: true, name: 'yiJiflmc', type: 'input', placeholder: '请输入' },
        { label: '二级分类代码', isDisabled: true, name: 'erJifldm', type: 'input', placeholder: '请输入' },
        { label: '二级分类名称', isDisabled: true, name: 'erJiflmc', type: 'input', placeholder: '请输入' },
        { label: '三级分类代码', isDisabled: true, name: 'sanJifldm', type: 'input', placeholder: '请输入' },
        { label: '医保代码', isDisabled: true, name: 'yiBaodm', type: 'input', placeholder: '请输入' },
        { label: '计量单位', isDisabled: true, name: 'jiLiangdw', type: 'input', placeholder: '请输入' },
        { label: '生产企业信用代码', isDisabled: true, name: 'shengChanqydm', type: 'input', placeholder: '请输入' },
        { label: '生产企业名称', isDisabled: true, name: 'shengChanqymc', type: 'input', placeholder: '请输入' },
        { label: '产地', isDisabled: true, name: 'chanDi', type: 'input', placeholder: '请输入' },
        { label: '价值属性', isDisabled: true, name: 'jiaZhisx', type: 'input', placeholder: '请输入' },
        { label: '大包装数', isDisabled: true, name: 'daBaozs', type: 'input', placeholder: '请输入' },
        { label: '中包装数', isDisabled: true, name: 'zhongBaozs', type: 'input', placeholder: '请输入' },
        { label: '小包装数', isDisabled: true, name: 'xiaoBaozs', type: 'input', placeholder: '请输入' },
        { label: '最小采购量', isDisabled: true, name: 'zuiXiaocgl', type: 'input', placeholder: '请输入' },
      ],
      s_formDatas: {
        sysSeq: 0,
        pid: '',
        keHuPid: null,
        keHumc: null,
        keHudm: null,
        xiaoShoucpPid: null,
        yiJifldm: null,
        yiJiflmc: null,
        erJifldm: null,
        erJiflmc: null,
        sanJifldm: null,
        sanJiflmc: null,
        yiBaodm: null,
        chanPingdm: null,
        pinMing: null,
        tongYongm: null,
        guiGe: null,
        xingHao: null,
        shuoMing: null,
        pingPai: null,
        jiLiangdw: null,
        wuShuidj: null,
        hanShuidj: null,
        shuiLv: null,
        shengChanqyPid: null,
        shengChanqymc: null,
        shengChanqydm: null,
        zhuCezhPid: null,
        zhuCezh: null,
        chanDi: null,
        jiaZhisx: null,
        daBaozs: null,
        zhongBaozs: null,
        xiaoBaozs: null,
        zuiXiaocgl: null,
        zhuangTai: null,
        zhaoPian1: null,
        zhaoPian2: null,
        zhaoPian3: null,
      },
      s_rules: {
        keHuPid: [{ required: true, message: '客户必须选择', trigger: 'blur' }],
        xiaoShoucpPid: [{ required: true, message: '请选择商品', trigger: 'blur' }],
        hanShuidj: [{ required: true, message: '含税单价必填', trigger: 'change' }],
      },
    }
  },
  computed: {},
  mounted() {},
  methods: {
    // 新增按钮 点击了新增
    addModal() {
      this.s_formDatas = {
        sysSeq: 0,
        pid: '',
        xiaoShoucpPid: null,
        keHuPid: null,
        zhongBiaolx: null,
        zhongBiaoh: null,
        yiJifldm: null,
        yiJiflmc: null,
        erJifldm: null,
        erJiflmc: null,
        sanJifldm: null,
        sanJiflmc: null,
        yiBaodm: null,
        chanPingdm: null,
        pinMing: null,
        tongYongm: null,
        guiGe: null,
        xingHao: null,
        shuoMing: null,
        pingPai: null,
        jiLiangdw: null,
        wuShuidj: null,
        hanShuidj: null,
        shuiLv: null,
        shengChanqyPid: null,
        shengChanqymc: null,
        shengChanqydm: null,
        zhuCezhPid: null,
        zhuCezh: null,
        chanDi: null,
        jiaZhisx: null,
        daBaozs: null,
        zhongBaozs: null,
        xiaoBaozs: null,
        zuiXiaocgl: null,
        zhuangTai: null,
        zhaoPian1: null,
        zhaoPian2: null,
        zhaoPian3: null,
      }
      console.log('点击了新增')
      this.modal.modalVisible = true
      this.modal.modalTitle = '新增商品对照'
    },
    // 新增按钮 点击了新增
    editModal(val) {
      console.log('点击了编辑')
      this.getSBypid(val)
      this.modal.modalVisible = true
      this.modal.modalTitle = '修改商品对照'
    },
    // 关闭模态详细框
    closeModal() {
      this.modal.modalVisible = false
      this.$emit('close')
    },
    // 保存成功关闭模态详细框
    closeModal2() {
      this.modal.modalVisible = false
      this.$emit('ok')
    },
    // 模态详细框提交按钮
    onSubmit(val) {
      this.saveObjInfo(val)
    },
    // 保存详细信息
    async saveObjInfo(val) {
      this.loading = true
      // this.$message.loading({ content: '提交中'， })
      let res = await saveInfo(val)
      this.loading = false
      console.log(res)
      if (res && res.code && res.code === 'C1' && res.data) {
        this.$message.success({ content: '提交成功', duration: 2 })
        this.closeModal2()
      } else {
        this.$message.error({ content: '提交失败', duration: 2 })
      }
    },
    // 得到初始化信息
    async getSBypid(val) {
      let res = await getInfoByPid(val)
      if (res && res.code && res.code === 'C1') {
        if (res.data) {
          this.s_formDatas = res.data
        } else {
          this.addModal()
        }
      }
      console.log(res)
    },
    // 表单编号
    onChangeType(name, val) {
      const _this = this
      switch (name) {
        case 'xiaoShoucpPid':
          _this.getSaleGoodsInfo(val)
          break
        case 'keHuPid':
          _this.getCustomerInfo(val)
          break
        case 'zhongBiaolx':
          _this.s_formDatas.zhongBiaolx = val
          break
      }
    },
    // 获取客户信息
    async getCustomerInfo(pid) {
      const _this = this
      const res = await getCustomerInfoByPid(pid)
      if (res && res.code && res.code === 'C1' && res.data) {
        _this.s_formDatas.keHuPid = res.data.pid
        _this.s_formDatas.keHuOrgPid = res.data.keHuOrgPid
        _this.s_formDatas.keHudm = res.data.keHudm
        _this.s_formDatas.keHumc = res.data.keHumc
        
      }
    },
    // 获取商品信息
    async getSaleGoodsInfo(pid) {
      const res = await getsaleGoodsInfoByPid(pid)
      if (res && res.code && res.code === 'C1' && res.data) {
        const newObj = {
          xiaoShoucpPid: res.data.pid,
          yiJifldm: res.data.yiJifldm,
          yiJiflmc: res.data.yiJiflmc,
          erJifldm: res.data.erJifldm,
          erJiflmc: res.data.erJiflmc,
          sanJifldm: res.data.sanJifldm,
          sanJiflmc: res.data.sanJiflmc,
          yiBaodm: res.data.yiBaodm,
          chanPingdm: res.data.chanPingdm,
          pinMing: res.data.pinMing,
          tongYongm: res.data.tongYongm,
          guiGe: res.data.guiGe,
          xingHao: res.data.xingHao,
          shuoMing: res.data.shuoMing,
          pingPai: res.data.pingPai,
          jiLiangdw: res.data.jiLiangdw,
          wuShuidj: res.data.wuShuidj,
          hanShuidj: res.data.hanShuidj,
          shuiLv: res.data.shuiLv,
          shengChanqyPid: res.data.shengChanqyPid,
          shengChanqymc: res.data.shengChanqymc,
          shengChanqydm: res.data.shengChanqydm,
          zhuCezhPid: res.data.zhuCezhPid,
          zhuCezh: res.data.zhuCezh,
          chanDi: res.data.chanDi,
          jiaZhisx: res.data.jiaZhisx,
          daBaozs: res.data.daBaozs,
          zhongBaozs: res.data.zhongBaozs,
          xiaoBaozs: res.data.xiaoBaozs,
          zuiXiaocgl: res.data.zuiXiaocgl,
          zhaoPian1: res.data.zhaoPian1,
          zhaoPian2: res.data.zhaoPian2,
          zhaoPian3: res.data.zhaoPian3,
          zhuangTai: res.data.zhuangTai,
        }
        this.s_formDatas = Object.assign(this.s_formDatas, newObj)
      }
    },
    //
    restData(val) {
      this.getSBypid(this.s_formDatas.pid)
    },
  },
}
</script>

<style>
</style>